package com.shrfid.rfidsdk.tag.uhf;


import com.shrfid.module_base.entity.TagEntity;
import com.shrfid.module_base.util.ByteUtils;
import com.shrfid.rfidsdk.tag.NewReadTagManager;
import com.shrfid.rfidsdk.tag.base.BaseResponseData;
import com.shrfid.rfidsdk.tag.base.BaseResponseFrame;

import java.util.ArrayList;
import java.util.List;


 public class UHFInventoryResponse extends BaseResponseData {


     @Override
     public void parseFrame(BaseResponseFrame frame) {
         setRollCode(frame.getRollCode());
         //07 00 01 01 01 00 1E 4B
         //07 00 01 01 01 00 1E 4B
         //盘点结束帧
         if (frame.getLen() == 7) {
             setComplete(true);
         } else {
             byte[] params = frame.getParams();
             List<TagEntity> data = getData();
             if (data == null) {//数据为空,创建list
                 data = new ArrayList<>();
             }
             //正确的 88730013290F00
             //15000103 01010C4224A60188730013290F000063 1DE2
             //错误的：4224A60188730013
             //正常的数据应该为16位(不包含校验位的2位),格式如下
             //11(len) 00(地址) 01(命令) 03(后面还有数据) 01(天线1) 01(EPC数量) 08(EPC长度) 01 02 00 A1 FD 33 49 51(EPC数据) 59(RSSI)
             if (params != null && params.length == 16) {
                 byte[] uid = new byte[7];
                 System.arraycopy(params, 7, uid, 0, 7);
                 String tagUid = ByteUtils.bytesToHexString(uid);
                 //信号强度
                 int rssi = params[15];
                 //天线ID
                 int antenna = params[0];
                 //过滤掉信号强度低的
                 if (rssi > 0) {

                 }
                 boolean isExist = false;
                 for (int i = 0; i < data.size(); i++) {
                     if (data.get(i).getUid().equals(tagUid)) {
                         isExist = true;
                         break;
                     }
                 }
                 System.out.println("uid = " + tagUid + ",isExist = " + isExist);
                 if (!isExist) {
//                     data.add(new TagEntity(tagUid, tagUid, NewReadTagManager.INSTANCE.rfidDecoder.iRFID_Decode96bit(uid)));
                     data.add(new TagEntity(tagUid, tagUid, tagUid));
                 }
             }
             setData(data);
         }
     }
}